package com.zwy.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Component
public class RedisIncrUtil {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private JedisPool jedisPool;

    /**
     * 自增
     * @param dbIndex
     * @param key
     * @return
     */
    public long incr(Integer dbIndex, String key){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.incr(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    /**
     * 自增按照增量
     * @param dbIndex
     * @param key
     * @param increment 增量
     * @return
     */
    public long incrBy(Integer dbIndex, String key, long increment){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.incrBy(key, increment);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    /**
     * 自增按照增量
     * @param dbIndex
     * @param key
     * @param increment 增量
     * @return
     */
    public double incrBy(Integer dbIndex, String key, double increment){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.incrByFloat(key, increment);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    /**
     * 按增量减
     * @param dbIndex
     * @param key
     * @param increment
     * @return
     */
    public double decrBy(Integer dbIndex, String key, long increment){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.decrBy(key, increment);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    /**
     * 自减
     * @param dbIndex
     * @param key
     * @return
     */
    public double decr(Integer dbIndex, String key){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.decr(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    public long hincrBy(Integer dbIndex, String key, String field, long increment){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.hincrBy(key, field, increment);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    public double hincrBy(Integer dbIndex, String key, String field, double increment){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.hincrByFloat(key, field, increment);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }
}
